package com.briup.day20;

public class Test_Synchronized {
    public static void main(String[] args) {
        TicketRunnable2 t=new TicketRunnable2();
        Thread th1=new Thread(t,"窗口1");
        Thread th2=new Thread(t,"窗口2");
        Thread th3=new Thread(t,"窗口3");
        th1.start();
        th2.start();
        th3.start();
    }
}
class TicketRunnable2 implements Runnable {
    private  int num=50;
    //定义锁对象
    Object mutex=new Object();
    @Override
    public void run() {
        while (true){
            //给需要同步操作的代码块上锁
            synchronized (mutex){
                if(num<=0){
                    break;
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                String name = Thread.currentThread().getName();
                System.out.println(name+"正在买票"+num);
                num--;
            }

        }

    }
}